﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using ESoft.DataAccess.Models;

namespace ESoft.DataAccess.EfCore
{
    /// <summary>
    /// SQl查询服务
    /// </summary>
    public abstract class BaseSqlQueryRepositry : ISqlRepositry
    {
        /// <summary>
        /// 
        /// </summary>
        protected readonly DataContext Db;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="db"></param>
        public BaseSqlQueryRepositry(DataContext db)
        {
            this.Db = db;
        }


        /// <summary>
        /// 获取首值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rawSql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public T ExecuteScalar<T>(string rawSql, params object[] parameters)
        {
            return Db.ExecuteScalar<T>(rawSql, parameters);
        }

        /// <summary>
        /// 异步获取查询首值
        /// </summary>
        /// <param name="rawSql"></param>
        /// <param name="parameters"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public Task<T> ExecuteScalarAsync<T>(string rawSql, params object[] parameters)
        {
            return Db.ExecuteScalarAsync<T>(rawSql, parameters);
        }

        /// <summary>
        /// 按SQl查询实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rawSql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public List<T> FindBySql<T>(string rawSql, params object[] parameters) where T : class, new()
        {
            return Db.ExecuteQuery<T>(rawSql, parameters);
        }

        /// <summary>
        /// 异步获取
        /// </summary>
        /// <param name="rawSql"></param>
        /// <param name="parameters"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public async Task<List<T>> FindBySqlAsync<T>(string rawSql, params object[] parameters) where T : class, new()
        {
            return await Db.ExecuteQueryAsync<T>(rawSql, parameters);
        }

        /// <summary>
        /// 分页SQL查询
        /// </summary>
        /// <param name="query">查询语句</param>
        /// <param name="pageInput">分页信息</param>
        /// <param name="orderBy">排序</param>
        /// <param name="parameters">参数项</param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public abstract PageResult<T> FindPageBySql<T>(string query, PageInput pageInput, string orderBy,
            params object[] parameters) where T : class, new();

        /// <summary>
        /// 分页获取SQL
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rawSql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public abstract Task<PageResult<T>> FindPageBySqlAsync<T>(string rawSql, PageInput pageInput, string orderBy,
            params object[] parameters) where T : class, new();
    }
}